/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utfpr.cp.model;

import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Adriano_2
 */
public class HTMLTagDAO extends DAO {

    public HTMLTagDAO(EntityManager em) {
        super(em);
    }
    
    public boolean saveAll(ArrayList<HTMLTag> list) {
        try {
            em.getTransaction().begin();
            for (HTMLTag tag : list) {
                em.persist(tag);
            }
            em.getTransaction().commit();
            return true;
        }
        catch (Exception e) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }
    
    public long getTotalTags() {
        try {
            Query q = em.createQuery("SELECT count(h) FROM HTMLTag h");
            return ((Long) q.getSingleResult() ).longValue();
        }
        catch (Exception e) {
            return -1;
        }
    }
    
    public long getTotalAttributes() {
        try {
            Query q = em.createQuery("select count(*) from HTMLTag h inner join h.atributos a");
            return ((Long) q.getSingleResult() ).longValue();
        }
        catch (Exception e) {
            return -1;
        }
    }
    
    public static HTMLTagDAO factory() {
        return new HTMLTagDAO(getEntity());
    }
}
